package com.calvin.lingting.actions;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView.OnEditorActionListener;
import com.calvin.lib.EventDispatch.CListen;
import com.calvin.lib.annotation.Notice;
import com.calvin.lib.annotation.ViewInject;
import com.calvin.lib.json.JSONArray;
import com.calvin.lib.json.JSONObject;
import com.calvin.lib.notification.NotificationCenter;
import com.calvin.lingting.MainActivity;
import com.calvin.lingting.R;
import com.calvin.lingting.adapter.BookListViewAdapter;
import com.calvin.lingting.config.LrtsConfig;
import com.calvin.lingting.config.NTConfig;
import com.calvin.lingting.service.SerializationCenter;
import com.calvin.lingting.utils.Lrts;

public class SearchAction implements OnEditorActionListener {

    JSONArray mBookList;

    EditText mEditText;

    @ViewInject(id = R.id.listView)
    ListView mBookListView;

    boolean mIsSearch = false;

    public SearchAction() {
        mBookList = new JSONArray();
    }

    @ViewInject(isTag = true, tag = "search_text", listen = CListen.INIT)
    public void onInit(View view) {
        if ("search_text".equals(view.getTag())) {
            mEditText = (EditText) view;
            mEditText.setOnEditorActionListener(this);

            mBookListView.setAdapter(new BookListViewAdapter(MainActivity.sharedCurrent(), mBookList));
            mBookListView.setOnScrollListener(new ScrorllListener());

            NotificationCenter.shared().scanNotice(this);
        }
    }

    @ViewInject(isTag = true, tag = {"nav_left_button", "search_button"})
    public void onNavLeftClick(View view) {
        if ("nav_left_button".equals(view.getTag())) {
            InputMethodManager inputMethodManager = (InputMethodManager) MainActivity.sharedCurrent().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mBookListView.getWindowToken(), 0);
            NotificationCenter.shared().removeNotice(NTConfig.NT_DS_SEARCH);
            NotificationCenter.shared().postNotice(NTConfig.NT_NAV_BACK);
        } else if ("search_button".equals(view.getTag())) {
            searchBook(mEditText.getText().toString());
        }
    }

    @ViewInject(id = R.id.listView, listen = CListen.ITEMCLICK)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        JSONObject bookInfo = mBookList.getJSONObject(position);
        String bookId = mBookList.getJSONObject(position).getStringValue(LrtsConfig.K_BOOK_ID);
        if (SerializationCenter.shared().getBookList().containsKey(bookId)) {
            bookInfo = SerializationCenter.shared().getBookList().getJSONObject(bookId);
        }
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_NEXT, R.layout.action_detail, new DetailAction(bookInfo));
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        searchBook(mEditText.getText().toString());
        return false;
    }

    @Notice(NTConfig.NT_DS_SEARCH)
    public void onSearchNotice(Object... argv) {
        JSONArray jsonArray = (JSONArray) argv[0];
        if (jsonArray.size() > 0) {
            mBookList.addAll(jsonArray);
            BookListViewAdapter adapter = (BookListViewAdapter) mBookListView.getAdapter();
            adapter.setBookList(mBookList);
            adapter.notifyDataSetChanged();
        }
        mIsSearch = jsonArray.size() > 0 ? jsonArray.size() != 10 : true;
    }

    public void searchBook(String word) {
        InputMethodManager inputMethodManager = (InputMethodManager) MainActivity.sharedCurrent().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(mBookListView.getWindowToken(), 0);
        mBookList.clear();
        mIsSearch = true;
        Lrts.shared().search(word, 1);
    }

    class ScrorllListener implements OnScrollListener {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem + visibleItemCount == totalItemCount && mBookList.size() > 0 && !mIsSearch) {
                mIsSearch = true;
                Lrts.shared().search(mEditText.getText().toString(), mBookList.size() / 10 + 1);
            }
        }

    }
}
